import {router} from '@/router/index';
import store from '@/store';
import iviewModal from '@/view/common/modal/Modal.vue'
const install = function (Vue) {
    const Bus = new Vue({
        methods: {
            emit (event, ...args) {
                this.$emit(event, ...args)
            },
            on (event, callback) {
                this.$on(event, callback)
            },
            off (event, callback) {
                this.$off(event, callback)
            },
            init (object, fields = []) {
                if (object.hasOwnProperty('id') && object['id'] !== '') {
                    return
                }
                for (let key in object) {
                    if (!fields.includes(key, 0)) {
                        object[key] = ''
                    }
                }
            },
            toName (name, query = {}) {
                router.push({
                    name: name,
                    query: query
                })
            },
            toPath (path, query = {}) {
                router.push({
                    path: path,
                    query: query
                })
            },
            closePage () {
                let name = store.state.app.currentPageName
                let pageOpenedList = store.state.app.pageOpenedList;
                let lastPageObj = pageOpenedList[0];
                let len = pageOpenedList.length;
                for (let i = 1; i < len; i++) {
                    if (pageOpenedList[i].name === name) {
                        if (i < (len - 1)) {
                            lastPageObj = pageOpenedList[i + 1];
                        } else {
                            lastPageObj = pageOpenedList[i - 1];
                        }
                        break;
                    }
                }
                store.commit('removeTag', name);
                store.commit('closePage', name);
                pageOpenedList = store.state.app.pageOpenedList;
                localStorage.pageOpenedList = JSON.stringify(pageOpenedList);
            },
            closePageToLink () {
                let name = store.state.app.currentPageName
                let pageOpenedList = store.state.app.pageOpenedList;
                let lastPageObj = pageOpenedList[0];
                let len = pageOpenedList.length;
                for (let i = 1; i < len; i++) {
                    if (pageOpenedList[i].name === name) {
                        if (i < (len - 1)) {
                            lastPageObj = pageOpenedList[i + 1];
                        } else {
                            lastPageObj = pageOpenedList[i - 1];
                        }
                        break;
                    }
                }
                store.commit('removeTag', name);
                store.commit('closePage', name);
                pageOpenedList = store.state.app.pageOpenedList;
                localStorage.pageOpenedList = JSON.stringify(pageOpenedList);
                this.linkTo(lastPageObj);
                store.state.app.close = false
            },
            linkTo (item) {
                let routerObj = {};
                routerObj.name = item.name;
                if (item.argu) {
                    routerObj.params = item.argu;
                }
                if (item.query) {
                    routerObj.query = item.query;
                }
                router.push(routerObj);
            },
            start () {
                this.$Loading.start();
            },
            finish () {
                this.$Loading.finish();
            },
            error () {
                this.$Loading.error();
            },
            alert (type = 'info', message, duration = 3) {
                if (type === 'info') {
                    this.$Message.info({
                        content: message,
                        duration: duration
                    });
                } else if (type === 'success') {
                    this.$Message.success({
                        content: message,
                        duration: duration
                    });
                } else if (type === 'warning') {
                    this.$Message.warning({
                        content: message,
                        duration: duration
                    });
                } else if (type === 'error') {
                    this.$Message.error({
                        content: message,
                        duration: duration
                    });
                }
            },
            modal (type, title = '系统提示', content = '') {
                if (type === 'info') {
                    this.$Modal.info({
                        title: title,
                        content: content
                    })
                } else if (type === 'success') {
                    this.$Modal.success({
                        title: title,
                        content: content
                    })
                } else if (type === 'warning') {
                    this.$Modal.warning({
                        title: title,
                        content: content
                    })
                } else if (type === 'error') {
                    this.$Modal.error({
                        title: title,
                        content: content
                    })
                } else if (type === 'confirm') {
                    this.$Modal.confirm({
                        title: title,
                        content: content
                    })
                }
            },
            // 加载
            load () {
                store.state.app.showModal = true
            },
            // 加载完成
            complete () {
                store.state.app.showModal = false
            },
            // 填充数据
            fillData (target, assign) {
                if (assign) {
                    Object.assign(target, assign)
                } else {
                    if (target.hasOwnProperty('id')) {
                        target['id'] = ''
                    }
                    this.init(target)
                }
            },
            // 获取登录用户
            getLoginUser () {
                return store.state.app.loginUser
            },
            // 页面离开确认
            routeLeave (next) {
                if (store.state.app.close) {
                    next()
                } else {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>未保存的信息将会丢失，确定离开该页面吗?</p>',
                        onOk: () => {
                            this.closePage()
                            store.state.app.close = false
                            next()
                        }
                    })
                }
            }
        }
    })
    Vue.prototype.$bus = Bus
}
export default install
